package com.csap.framework.oauth2;

import cn.dev33.satoken.LoginUtil;
import com.csap.framework.redis.RedisUtil;
import com.csap.framework.util.CollectionsUtil;
import com.csap.framework.util.StringUtil;
import com.google.common.collect.Sets;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.csap.framework.oauth2.UserInfoStoreService.PERMISSIONS;
import static com.csap.framework.oauth2.UserInfoStoreService.ROLE;
import static com.csap.framework.oauth2.UserInfoStoreService.USER_PERMISSIONS;

/**
 * @Author ycf
 * @Date 2023/6/12 21:00
 * @Version 1.0
 */
public interface IUserDataCache {
    /**
     * 获取 redis操作
     *
     * @return 结果
     */
    RedisUtil getRedisUtil();

    /**
     * 清除角色和权限缓存
     *
     * @param roleId 角色ID
     */
    default void clearRolePremissions(Long roleId) {
        if (Objects.isNull(roleId)) {
            return;
        }
        getRedisUtil().hdel(LoginUtil.getInstance().cachekeyRole(), roleId.toString());
        getRedisUtil().hdel(LoginUtil.getInstance().cachekeyPremissions(), roleId.toString());
    }

    /**
     * 清楚用户自己的角色和权限
     *
     * @param userId 用户Id
     */
    default void clearUserRolePremissions(String userId) {
        getRedisUtil().hdel(LoginUtil.getInstance().cachekey(userId), PERMISSIONS);
        getRedisUtil().hdel(LoginUtil.getInstance().cachekey(userId), ROLE);
    }

    /**
     * 获取角色权限
     *
     * @param roleIds  角色ID
     * @param function 函数
     * @return 结果
     */
    default Set<String> getRolePermissions(Set<String> roleIds, Function<String, Set<String>> function) {
        if (CollectionsUtil.isEmpty(roleIds)) {
            return Sets.newHashSet();
        }
        String key = LoginUtil.getInstance().cachekeyPremissions();
        Set<String> list = getRedisUtil().multiGet(key, roleIds)
                .stream()
                .filter(Objects::nonNull)
                .flatMap(i -> ((Set<String>) i).stream())
                .filter(StringUtil::isNotEmpty)
                .collect(Collectors.toSet());
        if (CollectionsUtil.isEmpty(list) || roleIds.size() != list.size()) {
            return function.apply(key);
        }
        return list;
    }

    /**
     * 获取用户权限
     *
     * @param userId   用户ID
     * @param function 函数
     * @return 结果
     */
    default Set<String> getUserPermissions(String userId, Function<String, Set<String>> function) {
        if (StringUtil.isEmpty(userId)) {
            return Sets.newHashSet();
        }
        String key = LoginUtil.getInstance().cachekey(userId);
        Set<String> list = (Set<String>) getRedisUtil().hget(key, USER_PERMISSIONS);
        if (Objects.isNull(list)) {
            return function.apply(key);
        }
        return list;
    }

    /**
     * 获取角色列表
     *
     * @param roleIds  角色ID
     * @param function 函数
     * @return 结果
     */
    default List<String> getRoleList(Set<String> roleIds, Function<String, List<String>> function) {
        String key = LoginUtil.getInstance().cachekeyRole();
        List<String> set = getRedisUtil()
                .multiGet(key, roleIds)
                .stream()
                .map(i -> (String) i)
                .filter(StringUtil::isNotEmpty)
                .collect(Collectors.toList());
        if (CollectionsUtil.isEmpty(set) || roleIds.size() != set.size()) {
            return function.apply(key);
        }
        return set;
    }
}
